// /rule/addRuleInfo.do
document.write('<script type="text/javascript" src="../static/js/back/utils.js"></script>');
document.write('<script type="text/javascript" src="../static/js/back/ajax.js"></script>');
var _url_ = '../crus/addAdminUserInfo.do';
var _url_edit = '../crus/updateAdminUserInfo.do';
var _map = {};
_showData = "";
var form = null ;
///首页
var _pageNum = 1;
var _type ;
  $(function(){
	  _type = getUrlParam("type");
	  console.log(_type)
	  if(_type === "add"){
		  getRuleListAdd();
		  $("input[name='userPwd']").parent().prev().find('span').show();
		  $("input[name='userPwd']").attr('lay-verify','required|pass');
	  }else if(_type === "edit"){
		  getRuleListAdd();
		  showEditData();
		  $("input[name='userPwd']").parent().prev().find('span').hide();
		  $("input[name='userPwd']").attr('lay-verify','');
	  }else{
		  getList();
	  }
  })
$(function(){
	  $("body").on("click",".mypage span",function(){
		  _pageNum = $(this).attr("data-status");
		  getList();
	  })
	  $("input[name='userPwd']").keyup(function(){
		  if(_type==="edit"){
			  if($(this).val()){
				  $(this).attr('lay-verify','required|pass');
			  }else{
				  $(this).attr('lay-verify','');
			  }
		  }
	})
	  
	  
});
  function getList(){
	  
	  _map["pageNum"] = _pageNum;
	  _map["userType"] = 1;
	  
	  _ajax_post_token('../crus/getUserList',_map,function(res){
		  if(!res.returnObject){
			  $(".layui-table tbody").html("");
			  $(".no-more-data").show();
			  return;
		  }else{
			  $(".no-more-data").hide();
		  }
		  console.log(res);
		  showPageInfo(res.returnObject);
		  $(".x-right").text("共有数据："+res.returnObject.total +" 条");
		  var html = "";
		  var status = "";
		  var statusHtml = "";
		  var statusHtmlTile = "";
		  
		  $.each(res.returnObject.list,function(i,v,arr){
			  if(v.userStatus == 1){
				  status = "正常";
				  statusHtml = ' <a onclick="member_stop(this,\''+v.id+'\')" href="javascript:;"  title="封号">'
 	             +'   <i class="iconfont">封停</i>'
	             +' </a>';
			  }else if(v.userStatus == 2){
				  status = '封停';
				  statusHtml = ' <a onclick="member_start(this,\''+v.id+'\')" href="javascript:;"  title="解封">'
	 	             +'   <i class="iconfont">解封</i>'
		             +' </a>';
			  }
			  html+=' <tr><td>'
//    	             +' <div class="layui-unselect layui-form-checkbox" lay-skin="primary" data-id="'+v.id+'" ><i class="layui-icon">&#xe605;</i></div>'
//    	             +' </td>                                                                                                                  '
//    	             +' <td>'
    	             +(i+1)+'</td>                                                                                                             '
    	             +' <td>'+v.userAccount+'</td>                                                                                           '
    	             +' <td>'+v.ruleName+'</td>                                                                                                      '
    	             +' <td>'+status+'</td>                                                                                                      '
    	             +' <td class="td-manage">                                                                                                 '
    	             +'   <a title="编辑"  onclick="x_admin_show(\'修改信息 - '+v.userAccount+'\',\'./admin-add.html?type=edit&id='+v.id+'\',455,400)" href="javascript:;">                       '
    	             +'     <i class="iconfont">修改</i>                                                                                 '
    	             +'   </a>                                                                                                                 '
    	             +'   <a title="删除" onclick="member_del(this,\''+v.id+'\')" href="javascript:;">                                         '
    	             +'     <i class="iconfont icon-delete x-red"></i>                                                                                 '
    	             +'   </a> '+ statusHtml +' </td> </tr>';
		  })
		  $(".layui-table tbody").html(html);
	  })
  }
      
  function getRuleListAdd(){
	  _ajax_post_token('../rule/getRuleGroupList',{pageSize:100},function(res){
		  console.log(res);
		  var list =  res.returnObject;
		  var html = '<option value="">请选择</option>';
		  $.each(list.list,function(i,v,arr){
    		  if(list){
    			 html+='<option value="'+v.id+'">'+v.title+'</option>';
    		  }
		  })
		  $(".ruleIds").html(html);
		  if(form){
			  console.log(form);
			  form.render();
		  }
	  })
  }
  function showEditData(){
	  _url_ = _url_edit;
	  var _id = getUrlParam("id");
	  $("#paramKey").val(_id);
	  console.log(_id);
	  _ajax_post_token('../crus/getUserById',{userType:1,id:_id},function(res){
		  console.log(res);
		  _showData = res.returnObject;
	  });
  }    
      
  /*用户-删除*/
  function member_del(obj,_id){
      layer.confirm('确认要删除吗？',function(index){
          //发异步删除数据
    	  _url_ =_url_edit;
    	  _ajax_post_token(_url_,{userStatus:0,id:_id},function(res){
    	      	console.log(res);
    	      	if(res.resultCode == 1000){
    	      		getList();
    	      	  layer.msg('删除成功!',{icon:1,time:2000});
    	      	}else{
    	      		layer.msg('删除失败!',{icon:1,time:2000});
    	      	}
    	      });
      });
  }
  /*用户-封停*/
  function member_stop(obj,_id){
	  layer.confirm('确认要封停此账号吗？',function(index){
		  //发异步删除数据
		  _url_ =_url_edit;
		  _ajax_post_token(_url_,{userStatus:2,id:_id},function(res){
			  console.log(res);
			  if(res.resultCode == 1000){
				  getList();
				  layer.msg('已封停!',{icon: 5,time:1000});
			  }else{
				  layer.msg('操作失败!',{icon:1,time:2000});
			  }
		  });
	  });
  }
  /*用户-启动*/
  function member_start(obj,_id){
	  layer.confirm('确认要解封此账号吗？',function(index){
		  //发异步删除数据
		  _url_ =_url_edit;
		  _ajax_post_token(_url_,{userStatus:1,id:_id},function(res){
			  console.log(res);
			  if(res.resultCode == 1000){
				  getList();
				  layer.msg('已解封!',{icon: 6,time:1000});
			  }else if (res.resultCode == 1008){
				  layer.msg('当前账号已存在',{icon:5,time:2000});
			  }
			  else{
				  layer.msg('操作失败!',{icon:5,time:2000});
			  }
		  });
	  });
  }
      
  function updateInfo(data){
	  //发异步，把数据提交给后台
	 console.log(_url_);
      _ajax_post_token(_url_,data,function(res){
      	console.log(res);
      	if(res.resultCode == 1000){
      	  parent.getList();
      	  parent.layer.msg('操作成功!',{icon:1,time:2000});
      	  x_admin_close();
          // 获得frame索引
      	}else{
      		layer.msg('操作失败!',{icon:1,time:2000});
      	}
      });
  }
      
      
      
      
      
      
      
//添加 和 修改窗口     